import { _decorator, Button, Component, EventHandler, Label, Node, } from 'cc';
import { InitCards } from './InitCards';
import { Player } from './Player';
const { ccclass, property } = _decorator;

@ccclass('CustomEvents')
export class CustomEvents extends Component {



    throwingSieve() {
        const sieves: number[] = []
        for (let index = 0; index < 2; index++) {
            sieves.push(Math.floor(Math.random() * 6) + 1)

        }

        const ininCards = this.node.getParent().getComponent(InitCards)

        ininCards.startDraw(sieves)

    }

    sendMessage(message) {
        // 触发 send-message 事件，发送消息
        console.log( this.node.emit('send-message', message));

        this.node.emit('send-message', message);
    }
    login() {

        const inputValue = this.node.getParent().getChildByName("Input").getChildByName('TEXT_LABEL').getComponent(Label).string
        console.log(inputValue);
        const url: string = "ws://loaclhost:7890/login"
        const data = {
            id: 1,
            username: inputValue
        };

        this.sendMessage(data)
        // 发送 POST 请求
        // fetch(url, {
        //     method: 'POST',                 // 请求方法
        //     headers: {
        //         'Content-Type': 'application/json'
        //     },
        //     body: JSON.stringify(data)      // 请求体
        // })
        //     .then(response => response.json())  // 解析 JSON 响应
        //     .then(data => {
        //         console.log('POST response data:', data);
        //         // 处理响应数据
        //     })
        //     .catch(error => {
        //         console.error('POST request error:', error);
        //     });
    }

}









